﻿using LightningTools.BCSTesterMan.Model;

namespace LightningTools.BCSTesterMan.ViewModel
{
    public class EntityViewModel : TreeViewItemViewModel
    {
        readonly Entity entity;
        private string bcsUrl;
        private string model;
        private MethodViewModel selectedMethodViewModel;

        /// <summary>
        /// Initializes a new instance of the <see cref="EntityViewModel"/> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public EntityViewModel(Entity entity) : base(null, true)
        {
            this.entity = entity;
        }

        /// <summary>
        /// Gets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return entity.Name; }
        }

        /// <summary>
        /// Gets the version.
        /// </summary>
        /// <value>The version.</value>
        public string Version
        {
            get { return entity.Version; }
        }

        /// <summary>
        /// Gets the namespace.
        /// </summary>
        /// <value>The namespace.</value>
        public string Namespace
        {
            get { return entity.Namespace; }
        }

        /// <summary>
        /// Gets the entity.
        /// </summary>
        /// <value>The entity.</value>
        public Entity Entity
        {
            get { return entity; }
        }

        /// <summary>
        /// Gets or sets the BCS URL.
        /// </summary>
        /// <value>The BCS URL.</value>
        public string BcsUrl
        {
            get { return bcsUrl; }
            set
            {
                bcsUrl = value;
                OnPropertyChanged("BcsUrl");
            }
        }

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public string Model
        {
            get { return model; }
            set
            {
                model = value;
                OnPropertyChanged("Model");
            }
        }

        /// <summary>
        /// Gets or sets the selected method view model.
        /// </summary>
        /// <value>The selected method view model.</value>
        public MethodViewModel SelectedMethodViewModel
        {
            get { return selectedMethodViewModel; }
            set
            {
                selectedMethodViewModel = value;
                OnPropertyChanged("SelectedMethodViewModel");
            }
        }

        /// <summary>
        /// Invoked when the child items need to be loaded on demand.
        /// </summary>
        protected override void LoadChildren()
        {
            foreach (var method in entity.Methods)
            {
                Children.Add(new MethodViewModel(method, this));
            }
        }

        /// <summary>
        /// Called when property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected override void OnPropertyChanged(string propertyName)
        {
            if (propertyName != null)
            {
                if (propertyName == "IsSelected" && IsSelected)
                {
                    Model = entity.Model;
                }
            }
            base.OnPropertyChanged(propertyName);
        }
    }
}